import { createRouter, createWebHistory } from 'vue-router'
import Login from "@/views/Login/index.vue"
import Home from "@/views/Home/index.vue"

import Message from "@/pulging/Message/js/Message.js"

import Register from "@/views/Register/index.vue"
import Person from "@/views/Person/index.vue"

import {useUserStore}  from "@/stores/user.js"

import {storeToRefs} from "pinia"

const router = createRouter({
  history: createWebHistory(),
  routes: [
  {path:"/",redirect:"/home"},
  {
    path:"/login",
    name:"login",
    component:Login
   },
   {
    path:"/home",
    name:"home",
    component:Home
   },
   {
    path:"/register",
    name:"register",
    component:Register
   },
   {
    path:"/person",
    name:"person",
    component:Person
   },
   {
    path:"/shoot",
    name:"shoot",
    component:()=> import("@/views/Shoot/index.vue")
   },
   {
    path:"/drill",
    name:"drill",
    component:()=> import("@/views/Drill/index.vue")
   }
  ]
})
router.beforeEach((to, from)=>{
  const userStore= useUserStore()
  const {isLogin} = storeToRefs(userStore)
  if (isLogin.value) {
      if (to.path == '/login') {
          return {path:"home"}
      } else {
        return 
      }
  } else {
    if (to.path == "/person") {
      console.log(333);
      Message({tyep:"error", text:"请先登录"})
      return {path:'/login'}
    }
  }
 return 
})
export default router
